package com.mjk.common.rest;


import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.web.session.HttpServletSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mjk.common.biz.BaseBiz;
import com.mjk.common.util.Query;
import com.mjk.common.vo.ObjectRestResponse;
import com.mjk.common.vo.TableResponse;

public class BaseController<Biz extends BaseBiz, Entity> {

	@Autowired
	protected HttpServletRequest request;
	@Autowired
	protected Biz baseBiz;
	
	@RequestMapping(value = "",method = RequestMethod.POST)
	@ResponseBody
	public ObjectRestResponse<Entity> add(@RequestBody Entity entity){
		baseBiz.insertSelective(entity);
		return new ObjectRestResponse<Entity>();
	}
	
	@RequestMapping(value="/{id}",method = RequestMethod.GET)
	@ResponseBody
	public ObjectRestResponse<Entity> get(@PathVariable int id){
		return new ObjectRestResponse<Entity>().flag(true).data(baseBiz.selectByPrimaryKey(id));
	}
	
	@RequestMapping(value="/{id}",method = RequestMethod.PUT)
	@ResponseBody
	public ObjectRestResponse<Entity> update(@RequestBody Entity entity){
		baseBiz.updateByPrimaryKeySelective(entity);
		return new ObjectRestResponse<Entity>();
	}
	
	@RequestMapping(value="/{id}",method = RequestMethod.DELETE)
	@ResponseBody
	public ObjectRestResponse<Entity> delete(@PathVariable int id){
		baseBiz.deleteByPrimaryKey(id);
		return new ObjectRestResponse<Entity>();
	}
	
	@RequestMapping(value = "/all",method = RequestMethod.GET)
    @ResponseBody
	public List<Entity> getAll(){
		return baseBiz.selectAll();
	}
	
	@RequestMapping(value = "/page",method = RequestMethod.GET)
    @ResponseBody
	public TableResponse<Entity> list(@RequestParam Map<String, Object> params){
		Query query = new Query(params);
		return baseBiz.selectByQuery(query);
	}
}
